package cn.iocoder.yudao.module.system.controller.admin.tiktok;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.URLUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.config.BaseConfig;
import cn.iocoder.yudao.module.system.config.TiktokProperties;
import cn.iocoder.yudao.module.system.dal.dto.tiktok.TiktokUserDto;
import cn.iocoder.yudao.module.system.service.tiktok.TiktokService;
import cn.iocoder.yudao.module.system.service.tiktok.TiktokUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import java.net.URI;
import java.net.URISyntaxException;

@Tag(name = "tiktok", description = "tiktok")
@Controller
@RequestMapping("tiktok")
public class TiktokController {

    @Resource
    private TiktokService tiktokService;

    @Resource
    private TiktokUserService tiktokUserService;

    @Resource
    private TiktokProperties tiktokProperties;

    @Resource
    private BaseConfig baseConfig;

    @Operation(summary = "构造授权链接")
    @PermitAll
    @GetMapping("auth")
    public ResponseEntity<Void> tiktokAuth() throws URISyntaxException {
        StringBuilder redirectUrl = new StringBuilder();
        long currentSeconds = DateUtil.currentSeconds();
        String state = currentSeconds + RandomUtil.randomString(4);
        redirectUrl.append(tiktokProperties.getAuthBase())
                .append("?client_key=").append(tiktokProperties.getClientKey())
                .append("&response_type=code")
                .append("&scope=").append(tiktokProperties.getAuthScope())
                .append("&redirect_uri=")
                .append(URLUtil.encode(tiktokProperties.getRedirectUri()))
                .append("&state=")
                .append(state);
        return ResponseEntity.status(HttpStatus.FOUND).location(new URI(redirectUrl.toString())).build();
    }

    @Operation(summary = "授权回调")
    @GetMapping("auth/callback")
    public ResponseEntity<Void> authCallBack(String code, String scopes, String error,
                                             String state,
                                             @RequestParam(name = "error_description", required = false) String errorDescription) throws URISyntaxException {
        tiktokService.getUserAuth(state, code, scopes, error, errorDescription);
        String redirectUrl = baseConfig.getFrontBaseUrl() + "/tiktok/info/index";
        return ResponseEntity.status(HttpStatus.FOUND).location(new URI(redirectUrl.toString())).build();
    }


    @Operation(summary = "查询tiktok社交信息")
    @GetMapping("userinfo")
    @ResponseBody
    public CommonResult<TiktokUserDto> tiktokUserInfo() {
        return CommonResult.success(tiktokUserService.getTiktokUserInfo());
    }
}